// pages/shop/shop.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: 1,
    pid: 1,
    page: 1,
    page_size: 10,
    uid: 9,
    category_id: 0,
    shop_list: [],
    nomoredata: false,
    category_list: [],
    current_index: 0,
    shoplist: [{
      id: 26
    },
    {
      id: 25
    }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init_data()
    let c_id = 0
    console.log(options.category)
    if (options && options.category) {
      c_id = options.category
      this.setData({
        current_index: c_id,
        category_id: options.category
      })
    }
    // let {
    //   index
    // } = options
    // let id = this.data.shoplist[index]['id']

    // this.setData({
    //   current_index: parseInt(index) + 1,
    //   category_id: options.category
    // })
    this.get_shop_list(c_id)
    this.get_category_list()
  },
  init_data() {
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
    })
  },
  get_category_list(index = 0) {
    let params = {
      id:this.data.id,
    }
    request.post(api.shopcategory_list, params).then(res => {
      // res.reverse()
      let category_all = {
        id: 0,
        visibility: 1,
        name: "全部推荐",
      }
      let category_list = []
      res.unshift(category_all)
      this.setData({
        category_list: res
      })
      let type = res[index].id
      // this.get_shop_list(type)
    })
  },
  choosing_category(e) {
    let current_index = e.currentTarget.dataset.index
    this.setData({
      current_index
    })
    let type = e.currentTarget.dataset.cate_id
    this.get_shop_list(type)
  },

  get_shop_list(type) {
    util.showLoading('正在加载')
    let category = type
    let params = {
      page: this.data.page,
      pageSize: 5,
      limit: 5,
      uid: this.data.uid,
      type,
      category
    }
    request.post(api.shoplist, params).then(res => {
      this.setData({
        shop_list: res
      })
      util.hideLoading()
    })
  },

  go_shop_detail(e) {
    let id = e.currentTarget.dataset.id
    if (!id) {
      id = 32
    }
    wx.navigateTo({
      url: '/pages/shop/detail?id=' + id,
    })
  },
//   get_shop_list() {
//     let params = {
//         page: 1,
//         limit: 20
//     }
//     request.post(api.shoplist, params).then(res => {
//       console.log(res)
//         this.setData({
//             shop_list: res
//         })
//     })
// },
  get_more_list() {
    let params = {
      id: this.id,
      page: this.data.page,
      pageSize: this.data.page_size,
      uid: this.data.uid
    }
    let old_list = this.data.shop_list
    request.post(api.shoplist, params).then(res => {
      if (res.length != 0) {
        let new_list = old_list.concat(res)
        this.setData({
          shop_list: new_list
        })
      } else {
        this.setData({
          noMoreData: true
        });
        util.showToast('暂无更多店铺了');
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var noMoreData = this.data.noMoreData;
    if (!noMoreData) {
      var pageno = this.data.page;
      pageno++;
      this.setData({
        page: pageno
      });
      this.get_more_list();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})